package com.arithmetic.version1;

import java.util.Random;

public class BinaryOperation {
    private int leftOperand;
    private int rightOperand;
    private char operator;
    private int value;

    public BinaryOperation() {}

    public BinaryOperation(int left, int right, char op) {
        this.leftOperand = left;
        this.rightOperand = right;
        this.operator = op;
        calculateValue();
    }

    private void calculateValue() {
        if (operator == '+') {
            value = leftOperand + rightOperand;
        } else if (operator == '-') {
            value = leftOperand - rightOperand;
        }
    }

    // 访问器方法
    public int getLeftOperand() { return leftOperand; }
    public int getRightOperand() { return rightOperand; }
    public char getOperator() { return operator; }
    public int getValue() { return value; }

    @Override
    public String toString() {
        return leftOperand + " " + operator + " " + rightOperand + " =  \t";
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        BinaryOperation that = (BinaryOperation) obj;
        return leftOperand == that.leftOperand &&
                rightOperand == that.rightOperand &&
                operator == that.operator;
    }

    // 生成方法
    public static BinaryOperation generateAdditionOperation() {
        Random rand = new Random();
        int left = rand.nextInt(100);
        int right = rand.nextInt(100);
        return new BinaryOperation(left, right, '+');
    }

    public static BinaryOperation generateSubstractOperation() {
        Random rand = new Random();
        int left = rand.nextInt(100);
        int right = rand.nextInt(left + 1); // 确保结果非负
        return new BinaryOperation(left, right, '-');
    }

    public static BinaryOperation generateBinaryOperation() {
        Random rand = new Random();
        if (rand.nextBoolean()) {
            return generateAdditionOperation();
        } else {
            return generateSubstractOperation();
        }
    }
}
